package com.th.providerprepaid_refill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lww.springcloudapi.pojo.TabHallUserbill;
import com.lww.springcloudapi.pojo.TabHallUsermessage;
import com.lww.springcloudapi.pojo.TabHallUserpackage;
import com.lww.springcloudapi.pojo.TabHallUserpayment;
import com.th.providerprepaid_refill.entity.vo.PrepaidRefillData;
import com.th.providerprepaid_refill.mapper.PrepaidRefillMapper;
import com.th.providerprepaid_refill.mapper.TabHallUserbillMapper;
import com.th.providerprepaid_refill.mapper.TabHallUserpackageMapper;
import com.th.providerprepaid_refill.service.PrepaidRefillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 缴费充值及缴费记录查询的业务层接口实现类
 */
@Service
public class PrepaidRefilServiceImpl implements PrepaidRefillService {

    /**
     * 注入操作 缴费实体 的类
     */
    @Autowired(required = false)
    private PrepaidRefillMapper prepaidRefillMapper;

    /**
     * 注入操作 账单实体 的类
     */
    @Autowired(required = false)
    private TabHallUserbillMapper userbillMapper;

    /**
     * 注入操作 用户套餐实体 的类
     */
    @Autowired(required = false)
    private TabHallUserpackageMapper userpackageMapper;

    /**
     * 缴费方法
     */
    @Override
    @Transactional //开启事务 将方法的执行作为一个事务
    public Integer recharge(PrepaidRefillData data) {

        data.setPaymenttime(new Date());

        QueryWrapper<TabHallUserpackage> wrapper = new QueryWrapper<TabHallUserpackage>();
        System.out.println(data.getPaymentamount());
        wrapper.eq("pnumber",data.getPnumber());
        //根据用户电话号码查询用户信息实体
        TabHallUserpackage userpackage = userpackageMapper.selectOne(wrapper);
//        System.out.println("进行充值的电话号码的用户id："+usermessage.getUserid());


        //将接收到的数据需要插入缴费表的数据录入缴费实体中
        TabHallUserpayment userpayment = new TabHallUserpayment();
        userpayment.setPaymentamount(data.getPaymentamount());
        userpayment.setPaymentmethod(data.getPaymentmethod());
        userpayment.setPaymenttime(data.getPaymenttime());
        userpayment.setPnumber(data.getPnumber());

        //插入缴费表
        prepaidRefillMapper.insert(userpayment);

        //修改用户套餐实体的余额使其5加上充值的金额
        userpackage.setMoney(userpackage.getMoney()+ data.getPaymentamount());
        //修改数据库中的用户套餐表数据
        userpackageMapper.updateById(userpackage);

        //创建一个账单实体
        TabHallUserbill userbill = new TabHallUserbill();
        userbill.setDeduction(data.getPaymentamount());
        userbill.setOperationtime(new Date());
        userbill.setOperation(data.getOperation());
        userbill.setPnumber(userpackage.getPnumber());

        //插入账单表
        userbillMapper.insert(userbill);

        return 1;
    }

    /**
     * 查询缴费记录
     */
    @Transactional //开启事务 将方法的执行作为一个事务
    public List<TabHallUserpayment> selectRefillRecording(String pnumber,Integer page,Integer limit){
        //根据传入的页码计算sql查询的开始下标
        Integer start = (page - 1) * limit;
        Integer offset = limit;
        //创建条件包装类
        QueryWrapper<TabHallUserpayment> wrapper = new QueryWrapper<TabHallUserpayment>();
        //设定条件
        wrapper.eq("pnumber",pnumber);

        wrapper.orderByDesc("paymenttime");
        String sql = "limit "+start+","+offset;
        wrapper.last(sql);

        //查询操作
        List<TabHallUserpayment> list = prepaidRefillMapper.selectList(wrapper);

        return list;
    }

    /**
     * 查询缴费记录总条数
     * @param pnumber
     * @return
     */
    public Integer selectCount(String pnumber) {
        //创建条件包装类
        QueryWrapper<TabHallUserpayment> wrapper = new QueryWrapper<TabHallUserpayment>();
        //设定条件
        wrapper.eq("pnumber",pnumber);

        //查询操作
        Integer count = prepaidRefillMapper.selectList(wrapper).size();
        return count;
    }
}
